package com.igeek.boot.config;

import com.igeek.boot.servlet.MyFilter;
import com.igeek.boot.servlet.MyListener;
import com.igeek.boot.servlet.MyServlet;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @Author fengqq
 * @Description 注入原生组件：方式二 RegistrationBean
 * @Date 2024/8/19  16:14
 *
 * 定制化的方式：
 * 方式一：修改配置文件
 *          xxxProperties类,可以在yml或者properties文件中 通过指定的前缀xxx来操作组件中的属性信息
 *          举例：
 *          @ConfigurationProperties(prefix = "server")
 *          public class ServerProperties{}
 *
 *          定制：
 *          server.port=8088
 *
 * 方式二：编写自定义的配置类  xxxConfiguration + @Bean替换、增加容器中默认组件、视图解析器
 *          举例：
 *          @Bean
 *          @ConditionalOnBean(MultipartResolver.class)
 *          @ConditionalOnMissingBean(name = DispatcherServlet.MULTIPART_RESOLVER_BEAN_NAME)
 * 		    public MultipartResolver multipartResolver(MultipartResolver resolver) {}
 *
 * 		    定制:
 * 		    @Configuration
 * 		    WebMVCConfig{
 *               @Bean
         *      public MultipartResolver multipartResolver() {
         *         CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver();
         *         //当前多部件解析器，不仅可以支持POST请求、也可以支持PUT请求 ==>默认只支持post请求，如果是restFul风格 需要支持put请求
         *         multipartResolver.setSupportedMethods("POST","PUT");
         *         //限制文件上传的大小
         *         multipartResolver.setMaxUploadSize(5242880);
         *         multipartResolver.setDefaultEncoding("UTF-8");
         *         return multipartResolver;
         *     }
 * 		    }
 * 方式三：XxxCustomizer 定制化器
 *      举例：
 *      @FunctionalInterface
 *      public interface WebServerFactoryCustomizer{}
 *

 * 定制的思路：
 *      1.在pom.xml中 引入相关的场景启动器
 *          Spring官网：spring-boot-starter-xxx
 *          各大第三方官网:xxx-spring-boot-starter
 *      2.查看xxx.autoconfigure包 xxxAutoconfiguration类
 *      3.xxxProperties类 做了哪些配置绑定？
 *        @Bean导入了哪些组件类？
 *      4.定制
 *
 */
@Configuration
public class MyRegistrationBeanConfig {
    //在IOC中注入原生的Servlet
    @Bean
    public ServletRegistrationBean<MyServlet> servletServletRegistrationBean(){
        ServletRegistrationBean<MyServlet> registrationBean =
                new ServletRegistrationBean<>(new MyServlet(), "/my", "/one");
        //随着tomcat容器的启动 会加载MyServlet
        registrationBean.setLoadOnStartup(1);
        return registrationBean;
    }
    //在IOC中注入原生的Filter
    @Bean
    public FilterRegistrationBean<MyFilter> filterFilterRegistrationBean(){
        FilterRegistrationBean<MyFilter> registrationBean = new FilterRegistrationBean<>();
        registrationBean.setFilter(new MyFilter());
        registrationBean.setUrlPatterns(Stream.of("/my").collect(Collectors.toList()));
        return registrationBean;
    }
    //在IOC中注入原生的Listener
    @Bean
    public ServletListenerRegistrationBean<MyListener> servletListenerRegistrationBean(){
        ServletListenerRegistrationBean<MyListener> registrationBean =
                new ServletListenerRegistrationBean<>(new MyListener());
        return registrationBean;
    }
}
